<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'process:log:pmWatermeterlog:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="40%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsProcessLogPmWatermeterlogForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { PmWatermeterlog, pmWatermeterlogSave, pmWatermeterlogForm } from '/@/api/process/log/pmWatermeterlog';
  import { pmProjectinformationTreeData } from '/@/api/proj/info/pmProjectinformation';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('process.log.pmWatermeterlog');
  const { showMessage } = useMessage();
  const record = ref<PmWatermeterlog>({} as PmWatermeterlog);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增表务日志') : t('编辑表务日志'),
  }));

  const inputFormSchemas: FormSchema[] = [
  {
      label: t('项目名称'),
      field: 'projectno',
      fieldLabel: 'projInfo.projectname',
      component: 'TreeSelect',
      componentProps: {
        api: pmProjectinformationTreeData,
        params: {
          isShowCode: true,
        },
        canSelectParent: false,
        allowClear: false,
        // onChange: async (e) => {
        //   queryParams.value = {
        //     pcode: e,
        //   };
        //   let arr = await pmMaterialneedsTable.getDataSource();
        //   let arrNew = [...arr];
        //   arrNew.forEach(async (item, index) => {
        //     console.log(index);
        //     // console.log(item,'111');
        //     await pmMaterialneedsTable.deleteTableDataRecord(item);
        //     // console.log(item,'2222');
        //   });
        // },
      },
      itemProps: { validateTrigger: 'blur' },
      required: true,
    },
    {
      label: t('项目编号'),
      field: 'projectno',
      component: 'Input',
      dynamicDisabled: true,
      componentProps: {
        maxlength: 50,
        // readonly: true,
      },
      required: true,
    },
    // {
    //   label: t('项目编号'),
    //   field: 'projectno',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 50,
    //   },
    // },
    {
      label: t('操作时间'),
      field: 'logtime',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        // showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('操作人'),
      field: 'duser',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    
    {
      label: t('操作类型'),
      field: 'dtype',
      component: 'Select',
      componentProps: {
        dictType: 'zf_waterLog_operType',
        allowClear: true,
      },
      // rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('操作内容'),
      field: 'devent',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 255,
      },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await pmWatermeterlogForm(data);
    record.value = (res.pmWatermeterlog || {}) as PmWatermeterlog;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
        createDate: record.value.createDate,
        name: record.value.name,
        dtimestamp: record.value.dtimestamp,
      };
      // console.log('submit', params, data, record);
      const res = await pmWatermeterlogSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
